package parsing.yago;

import java.io.File;

import core.exceptions.ParserException;


public class YagoParser {

	private static YagoParser _parser = null;
	
	/* flags to indicate which parts have we already done
	 * successfully. see getParser() method and explanation
	 * of singleton design pattern to under why we use these */
	private static boolean _taxamonyFileRead = false;
	private static boolean _transitiveTypeFileRead = false;
	private static boolean _literalFactsFileRead = false;
	private static boolean _wikipediaInfoFileRead = false;
	private static boolean _wikipediaThumbnailURLdone = false;
	private static boolean _labelsFileRead = false;
	
	/* directory where files are placed. If null, current directory is taken */
	private String _directory = null;
	
	/* prevent instantiation of this class: */
	private YagoParser() { }
	
	/* Singleton design pattern: allow only one instance of YagoParser
	 * to exist in project. If first call, instantiate it.
	 * This way only one parsing can be done at a time, and should
	 * parsing fail in the middle (for instance, user placed a
	 * wrong file in directory, we can just pick up where we left
	 * off, no need to do everything from scratch */ 
	public static YagoParser getParser() {
		if (_parser == null)
			_parser = new YagoParser();
		return _parser;
	}
	
	/* sets directory for files to parse (all files must be present in this directory) */
	public void setDirectory(String dir) {
		_directory = dir;
	}
	
	public void setParserProgress(IParserProgress parserProgress) {
		ParsingEnvironment.setParserProgress(parserProgress);
	}
	
	/* main method which will handle all Yago parsing: */
	public void parse() throws ParserException {
		
		/* first resolve directory, if null use current: */
		if (_directory == null) {
			_directory = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
		}
		
		/* if no slash at end, add it: */
		if ((_directory.charAt(_directory.length() - 1) + "") != File.separator)
			_directory = _directory.concat(File.separator);
		
		/* start by parsing Taxonomy file. From this file we will extract
		 * book categories and publishing companies */
		if (!_taxamonyFileRead) {
			new TaxonomyParser(_directory).parseFile();
			_taxamonyFileRead = true;
		}

		/* now parse TransitiveTypes files and extracts books, writers,
		 * countries, cities, publishers and connect books to categories: */
		if (!_transitiveTypeFileRead) {
			new TransitiveTypeParser(_directory).parseFile();
			_transitiveTypeFileRead = true;
		}
		
		/* now parse LiteralFacts and extract data about the books (i.e. page
		 * count, ISBN and so on) and about the writers (birthday and whatever) */
		if (!_literalFactsFileRead) {
			new LiteralFactsParser(_directory).parseFile();
			_literalFactsFileRead = true;
		}
		
		/* now parse WikipediaInfo and link between different objects, mainly
		 * between country and city, book and writer and so on*/
		if (!_wikipediaInfoFileRead) {
			new WikipediaInfoParser(_directory).parseFile();
			_wikipediaInfoFileRead = true;
		}
		
		WikipediaThumbnailURLGrabber grabber = new WikipediaThumbnailURLGrabber();
		if (!_wikipediaThumbnailURLdone) {
			new WikipediaThumbnailURLGrabber().grab();
			_wikipediaThumbnailURLdone = true;
		}
		
		/* now parse Labels and remove duplicated by comparing objects'
		 * preferredname attribute */
		if (!_labelsFileRead) {
			new LabelsParser(_directory).parseFile();
			_labelsFileRead = true;
		}
		
		grabber.waitForAllTasksToBeDone();
	}
}
